package com.dayu.demo.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.vecspace.data.model.EntityBO;
import com.vecspace.data.model.ListExBO;
import com.vecspace.data.model.FilterColumn;
import com.vecspace.dicttable.DictAll;
import com.vecspace.jsmodel.ZTreeNode;
import com.vecspace.msoffice.ExcelTemplate;
import com.vecspace.utils.Convert;
import com.vecspace.web.servlet.model.BaseController;
import com.vecspace.web.servlet.model.HController;
import com.vecspace.web.servlet.model.Param;
import com.vecspace.web.servlet.model.Path;
import com.vecspace.constant.AuthTag;
import com.vecspace.database.model.QueryConfig;
import com.dayu.demo.pojo.Department;
import com.dayu.demo.pojo.DepartmentEx;
import com.dayu.demo.pojo.DepartmentStats;
import com.dayu.demo.service.DepartmentService;

/** 部门 Controller
 * 在Controller中进行与页面相关的处理，比如把数据转换为HTML格式等。
 * @template dayu-code-builder
 * @author user
 * @reviewer
 * @createTime 2021-11-24 15:11:41
 * @version 1.0
*/
@HController("department")
public class DepartmentController extends BaseController {

    /**根据ID查询 
     * @return Department
     * @throws Exception
     * @author user
     * @reviewer 
     * @createTime 2021-11-24 15:11:41
     */
    @Path("getDepartment")
    public Department getDepartment(@Param("id") Long id) throws Exception {
        return departmentService.getDepartment(id);
    }
    
    @Path("getDepartmentBO")
    public EntityBO<Department> getDepartmentBO(@Param("id") Long id) throws Exception {
        Department department = departmentService.getDepartment(id);
        EntityBO<Department> entityBO = new EntityBO<Department>(department);
        return entityBO;
    }

    /**根据父节点id获取子节点
     *
     * @param nodeId ztree的父节点id，znode.id
     * @return
     * @throws Exception
     */
    @Path(value = "getChildNodes")
    public List<ZTreeNode> getChildNodes(@Param("pId")String nodeId) throws Exception {
    	Long topId = -1L;
        Long parentId = topId;
        String nodeType = "cat";//nodeId的前缀
        if(StringUtils.isBlank(nodeId)){
            nodeType = "cat";
            parentId = topId;
        }else{
            int spIdx = nodeId.indexOf("-");
            if(spIdx >= 0){
                nodeType = nodeId.substring(0, spIdx);
                parentId = Convert.toLong(nodeId.substring(spIdx+1), -1L);
            }else{
                nodeType = "cat";
                parentId = Convert.toLong(nodeId, -1L);
            }
        }

        List<ZTreeNode> znodes = new ArrayList<>();
        if("cat".equals(nodeType)) {
            //获取下级部门
            List<Department> departmentList = departmentService.getChildDepartmentList(parentId);
            for(Department department : departmentList){
                ZTreeNode znode = new ZTreeNode();
                //对id增加前缀，区分不同的数据对象
                znode.setId("cat-"+department.getId());
                znode.setpId(nodeId);
                znode.setName(department.getName());
                znode.setIsParent(true);
                znode.setOrdinalNumber(department.getOrdnum());
                znodes.add(znode);
            }

        }
        return znodes;
    }

    /** 插入或修改  
     * @param item    要保存的对象
     * @return void
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    @Path("saveDepartment")
    public EntityBO<Department> saveDepartment(@Param("item") Department item) throws Exception {
        //TODO：增加权限判断，是否可以新增、编辑
        if (item == null){
            return null;
        }
        /*** 对item对象进行校验，如必填项，格式，逻辑关系等 ***/
        Department department = departmentService.saveViewDepartment(item);
        return getDepartmentBO(department.getId());
    }



    /**更改 自己所属的父节点
     *
     * @param id T_DEMO_DEPARTMENT.id
     * @param parentIdNew 新的 上级部门ID
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    @Path("changeParent")
    public void changeParent(@Param("id")Long id, @Param("parentIdNew")Long parentIdNew) throws Exception {
        departmentService.changeParent(id, parentIdNew);
    }

    /**修改数据锁定状态
     * @param id 记录id，另-1表示对整表进行锁定
     * @param lockState 锁定状态，参考 DataLockState
     * @author wanghua
     * @date 2021/11/17  20:45
     * @return void
     **/
    @Path("changeLockState")
	public void changeLockState(@Param("id")Long id, @Param("lockState")int lockState) throws Exception {
        int authTag = AuthTag.All;//从权限系统获取操作权限类型
        departmentService.changeLockState(id, lockState, authTag);
    }

    /** 根据ID删除 
     * @param id    主键值
     * @return void
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-24 15:11:41
     */
    @Path("deleteDepartment")
    public void deleteDepartment(@Param("id") Long id) throws Exception {
        //TODO：增加权限判断，是否可以删除
        departmentService.deleteDepartment(id);
    }

    /**根据查询条件获取查询结果（分页查询），同时进行聚合统计查询
     * 
     
     
     * @param filters    查询条件（字段(属性)和值）
     * @param queryConfig  分页和排序参数
     * @return PageModel
     * @throws Exception
     * @author
     * @createTime 2021-10-05 23:16:31
     */
    @ExcelTemplate(templateFileName = "department.xlsx")
    @Path("getDepartmentExListAndStats")
    public ListExBO<DepartmentEx, Void, DepartmentStats> getDepartmentExListAndStats(@Param("filters") ArrayList<FilterColumn> filters,
          @Param("queryConfig") QueryConfig queryConfig) throws Exception {

        ListExBO<DepartmentEx, Void, DepartmentStats> departmentBOList = departmentService.getDepartmentExListAndStats(filters, queryConfig.getPageIndex(), queryConfig.getPageSize(), queryConfig.getSortName(), queryConfig.getSortOrder());


        return departmentBOList;
    }
    
    /************* property ********************/
    @Autowired
    private DepartmentService departmentService;


}
